summaryrefslogtreecommitdiff
path: root/src/options.cpp
blob: 59dd2b4498855c238872e1ee7a615772083d6407 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include "options.h"

#include "unitnumber.h"
#include "number.h"

#include <bu/sio.h>
#include <stdlib.h>

Options::Options( int argc, char *argv[] ) :
    iScale( 0 ),
    iRadix( 10 )
{
    addOption( iRadix, 'r', "radix",
        Bu::String("Set the radix (default: %1)").arg( iRadix ) );
    addOption( iScale, 's', "scale",
        Bu::String("Set the scale (default: %1)").arg( iScale ) );
    addOption( Bu::slot(this, &Options::selfTest), "self-test",
        "Run a series of tests to ensure everything is working correctly.");
    addOption( Bu::slot(this, &Options::textPrimes), "text-primes",
        "Generate primes in base 36 that only have digits > 9 in them.");
    addOption( Bu::slot(this, &Options::isPrime), 'p', "is-prime",
        "Tests every parameter after to see if it is prime then prints out "
        "the ones that are prime.  Set radix first.");
    addHelpOption('h', "help", "This help");

    parse( argc, argv );
}

Options::~Options()
{
}

int Options::selfTest( Bu::StringArray )
{
    UnitNumber().run();

    exit( 0 );
    return 0;
}

bool hasDigits( const Bu::String &s )
{
    for( Bu::String::const_iterator i = s.begin(); i; i++ )
    {
        if( *i >= '0' && *i <= '9' )
            return true;
    }
    return false;
}

int Options::textPrimes( Bu::StringArray )
{
    Number tst( 0, 36 );
    Number max( 0, 36 );
    Number j( 0, 36 );
    Number one( 0, 36 );
    Number fact( 0, 36 );
    one = "1";
    fact = "2";

    for( tst = "1";; tst = tst + one )
    {
        if( hasDigits( tst.toString() ) )
            continue;

        max = tst / fact;
        bool bPrime = true;
        for( j = "2"; j < max; j = j + one )
        {
            if( (tst%j).isZero() )
            {
                bPrime = false;
                break;
            }
        }

        if( bPrime )
        {
            Bu::println("%1").arg( tst );
        }
    }
    
    exit( 0 );
    return 0;
}

int Options::isPrime( Bu::StringArray aArgs )
{
    Number tst( 0, iRadix );
    Number max( 0, iRadix );
    Number j( 0, iRadix );
    Number one( 0, iRadix );
    Number fact( 0, iRadix );
    one = "1";
    fact = "2";

    for( Bu::StringArray::iterator i = aArgs.begin()+1; i; i++ )
    {
        tst = *i;
        max = tst / fact;
        bool bPrime = true;
        for( j = "2"; j < max; j = j + one )
        {
            if( (tst%j).isZero() )
            {
                bPrime = false;
                break;
            }
        }

        if( bPrime )
        {
            Bu::println("%1").arg( tst );
        }
    }

    exit( 0 );
    return aArgs.getSize();
}